<?php

namespace backend\modules\personnel\controllers;

use common\models\personnel\Members;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\personnel\Personnel;
use common\enums\StatusEnum;
use backend\controllers\BaseController;

/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class PerController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Personnel::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        //var_dump(Yii::$app->request->queryParams);die();
        $group =Yii::$app->request->get('group');
       // var_dump($group);die();
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['name'], // 模糊查询
            'defaultOrder' => [
                'personnel_id' => SORT_DESC,
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'group'=>$group,
            'menuDropDownLists' => Yii::$app->services->menu->getDropDowns(),
            'menuDropDownList' => Yii::$app->services->menu->getPosition(),
        ]);
    }

    public function actionUpper()
    {
        $id = Yii::$app->request->get();
        $arr =Members::find()

           ->where(['group'=>$id['group']])
           ->asArray()
           ->all();
        $personnel_id =array_column($arr,'personnel_id');//返回这个小组中所有成员id
       //判断是否有相同的人加入这个小组
     if(in_array($id['id'],$personnel_id)){

         return $this->message("不能重复添加", $this->redirect(Yii::$app->request->referrer), 'error');

     }
        $data = new Members();
        $data->personnel_id=$id['id'];
        $data->group=$id['group'];
        if($data->save()){
            return $this->message("添加成功", $this->redirect(Yii::$app->request->referrer));
        }
    }
}